package com.guigu.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.qingcheng.mapper.TbCategoryReportMapper;
import com.guigu.qingcheng.pojo.TbCategoryReport;
import com.guigu.qingcheng.pojo.dto.BrowseCategory;
import com.guigu.qingcheng.pojo.dto.CategoryReportDto;
import com.guigu.qingcheng.pojo.dto.OrderBrowse;
import com.guigu.qingcheng.pojo.dto.ReturnOrderDto;
import com.guigu.qingcheng.pojo.vo.CategoryReportVo;
import com.guigu.qingcheng.service.TbCategoryReportService;
import com.guigu.qingcheng.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;


/**
*
* @author Jiang
* @date 2020/07/31
*/
@Service
public class TbCategoryReportServiceImpl implements TbCategoryReportService {
    @Autowired(required = false)
    private TbCategoryReportMapper tbCategoryReportMapper;


    @Override
    public List<TbCategoryReport> queryAll() {
        List<TbCategoryReport> tbCategoryReport = tbCategoryReportMapper.selectAll();
            return tbCategoryReport;
    }

    @Override
    public PageUtil<TbCategoryReport> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbCategoryReport> pages = (Page<TbCategoryReport>) tbCategoryReportMapper.selectAll();
        PageUtil<TbCategoryReport> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbCategoryReport> pageQueryByAll(Integer pageNum, Integer pageSize, TbCategoryReport tbCategoryReport) {

        Example example=new Example(TbCategoryReport.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        //if(!StringUtils.isEmpty(brand.getId())){
        //    criteria.andEqualTo("id",brand.getId());
        //}
        //if(!StringUtils.isEmpty(brand.getName())){
        //   criteria.andLike("name","%"+brand.getName()+"%");
        //}
        PageHelper.startPage(pageNum,pageSize);
        Page<TbCategoryReport> pages = (Page<TbCategoryReport>) tbCategoryReportMapper.selectByExample(example);
        PageUtil<TbCategoryReport> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean saveTbCategoryReport(TbCategoryReport tbCategoryReport) {
        int insert = tbCategoryReportMapper.insert(tbCategoryReport);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbCategoryReport(TbCategoryReport tbCategoryReport) {
        int i = tbCategoryReportMapper.updateByPrimaryKeySelective(tbCategoryReport);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbCategoryReportMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbCategoryReport findById(Integer id) {
        TbCategoryReport tbCategoryReport = tbCategoryReportMapper.selectByPrimaryKey(id);
        return tbCategoryReport;
    }

    @Scheduled(cron = "0 0 1 * * ?")
    @Transactional
    @Override
    public List<TbCategoryReport> QueryDateByAll() {
        // 得到昨天的日期
        LocalDate localDate= LocalDate.now().minusDays(1);
        List<TbCategoryReport> tbCategoryReports = tbCategoryReportMapper.QueryDateByAll(localDate);
        for (TbCategoryReport tbCategoryReport:tbCategoryReports){
            tbCategoryReportMapper.insertSelective(tbCategoryReport);
        }
        return tbCategoryReports;
    }

    @Override
    public List<CategoryReportDto> queryDateList(CategoryReportVo categoryReportVo) {
        List<CategoryReportDto> tbCategoryReports= tbCategoryReportMapper.queryDateList(categoryReportVo);
        return tbCategoryReports;
    }


}
